package com.qianguo.shop.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.utils.IconfontUtil;
import com.qianguo.shop.R;
import com.qianguo.shop.base.BaseTitleActivity;
import com.eva.androidlibrary.icon.FruitIcon;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;
import com.qianguo.shop.constant.ShopKeys;
import com.qianguo.shop.constant.SexCode;
import com.qianguo.shop.entity.Mailmen;
import com.qianguo.shop.entity.MailmenListBean;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 配送员管理
 *
 * @author GUXIA318
 * @date 2015-10-19 16:29
 */
@EActivity(R.layout.activity_mailmen)
public class MailmenManagerActivity extends BaseTitleActivity {

    @ViewById(R.id.mailmen_status_text_view)
    TextView mTvStatus;

    @ViewById(R.id.mailmen_recycler_view)
    SwipeRefreshRecyclerView mSwipeRefreshRecyclerView;

    @Extra(ShopKeys.EXTRA_KEY_SHOP_ID)
    String mShopID;

    private static final int PER_PAGE = 10;
    private int mCurrentPage = 1;
    private int mTotalNum = -1;
    private MailmenAdapter mAdapter;
    private List<Mailmen> mMailmenItems = new ArrayList<>();

    public static void jumpToMailmenManager(Context context, String shopID) {
        Intent intent = new Intent();
        intent.putExtra(ShopKeys.EXTRA_KEY_SHOP_ID, shopID);
        intent.setClass(context, MailmenManagerActivity_.class);
        context.startActivity(intent);
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.mailmen_title);
    }

    @Override
    protected void initializeUI() {
        mAdapter = new MailmenAdapter(this);
        mSwipeRefreshRecyclerView.setAdapter(mAdapter);

        mSwipeRefreshRecyclerView.setSwipeRefreshListener(new SwipeRefreshRecyclerView.SwipeRefreshListener() {
            @Override
            public void onLoadMore() {
                if (mCurrentPage * PER_PAGE < mTotalNum) {
                    getMailmanList(mShopID, ++mCurrentPage, PER_PAGE);
                }
            }

            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mMailmenItems.clear();
                getMailmanList(mShopID, mCurrentPage, PER_PAGE);
            }
        });

        getMailmanList(mShopID, mCurrentPage, PER_PAGE);
    }

    @Background
    void getMailmanList(String id, int page, int perPage) {
        mApp.getApi().getMailmanList(mApp.getUser().getToken(), id, page, perPage, new HttpRequestCallback<MailmenListBean>() {
            @Override
            public void onSuccess(int code, String msg, MailmenListBean data) {
                if (data != null) {
                    if (mTotalNum < 0) {
                        mTotalNum = data.getTotal();
                    }

                    if (data.getList() != null) {
                        mMailmenItems.addAll(data.getList());
                    }

                    renderUI();
                }

            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(MailmenManagerActivity.this, msg);
            }
        });

    }

    @UiThread
    void renderUI() {
        mSwipeRefreshRecyclerView.onCompleted();
        mAdapter.notifyDataSetChanged();
    }


    public class MailmenAdapter extends RecyclerView.Adapter<MailmenViewHolder> {

        private Context mContext;
        public MailmenAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public MailmenViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_mailmen, null);
            v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new MailmenViewHolder(v);
        }

        @Override
        public void onBindViewHolder(MailmenViewHolder holder, int i) {
            holder.onBind(mMailmenItems.get(i));
        }

        @Override
        public int getItemCount() {
            return mMailmenItems != null ? mMailmenItems.size() : 0;
        }
    }

    public class MailmenViewHolder extends RecyclerView.ViewHolder {

        ImageView mImgAvatar;
        TextView mTvName;
        TextView mTvMobile;
        TextView mTvDealCount;
        TextView mTvSex;

        public MailmenViewHolder(View itemView) {
            super(itemView);
            mImgAvatar = (ImageView) itemView.findViewById(R.id.mailmen_avatar_image_view);
            mTvName = (TextView) itemView.findViewById(R.id.mailmen_name_text_view);
            mTvMobile = (TextView) itemView.findViewById(R.id.mailmen_mobile_text_view);
            mTvDealCount = (TextView) itemView.findViewById(R.id.mailmen_deal_count_text_view);
            mTvSex = (TextView) itemView.findViewById(R.id.mailmen_sex_text_view);
        }

        public void onBind(Mailmen item) {
            mImageLoader.loadImage(mContext, mImgAvatar, item.getAvatar());
            mTvName.setText(item.getRealName());
            mTvMobile.setText(item.getPhone());
            mTvDealCount.setText(getString(R.string.shop_assistant_item_deal_count_template, item.getCourierSevenOrder()));
            if (item.getSexCode() == SexCode.FEMALE) {
                IconfontUtil.setIcon(mContext, mTvSex, FruitIcon.ICON_FEMALE);
            } else {
                IconfontUtil.setIcon(mContext, mTvSex, FruitIcon.ICON_MALE);
            }
        }
    }
}
